package com.stockz.equity.charting.view.widget;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import javax.swing.DefaultComboBoxModel;

import org.springframework.aop.target.dynamic.Refreshable;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.DependsOn;
import org.springframework.core.Ordered;
import org.springframework.stereotype.Component;

import com.stockz.downloader.DownloadManager;
import com.stockz.equity.charting.event.EventPublisher;
import com.stockz.equity.charting.event.RefreshStockChartDataEvent;
import com.stockz.equity.charting.view.StockChartView;
import com.stockz.equity.charting.view.toolbar.NavigationToolbar;
import com.stockz.equity.charting.view.widget.custom.FilteredComboBox;
import com.stockz.equity.charting.view.widget.custom.FilteredComboBox.OnSelectionCallback;
import com.stockz.equity.manager.ScripManager;
import com.stockz.equity.model.Scrip;
import com.stockz.equity.pattern.Child;

@Component
@DependsOn("scripManagerImpl")
public class ScripNameComboBox extends FilteredComboBox implements Child, Ordered,
		Refreshable, OnSelectionCallback,InitializingBean {
	private static final long serialVersionUID = 8221076684576592103L;

	@SuppressWarnings("unchecked")
	private List<Class> parentList = new ArrayList<Class>(1);
	private List<Scrip> scripList;

	@Autowired private ScripManager scripManager;
	@Autowired private DownloadManager downloadManager;
	@Autowired StockChartView stockChartView;
	
	public ScripNameComboBox(){
		parentList.add(NavigationToolbar.class);
		super.setOnSelectionCallback(this);
	}
	
	@Override
	public void afterPropertiesSet() throws Exception {
		reset();
	}
	
	public void reset(){
		scripList = scripManager.getAllScripList(downloadManager.getLastDownloadDate());
		if(scripList != null && !scripList.isEmpty()){
			Collections.sort(scripList, new Comparator<Scrip>(){
				@Override
				public int  compare(Scrip o1, Scrip o2) {
					return o1.getScripName().compareTo(o2.getScripName());
				}
			});
			setModel(new DefaultComboBoxModel(scripList.toArray()));
		}
	}
	
	
	
	
	@Override
	public void refresh() {
		
	}
	
	
	
	@Override
	public void onSelection(Object selectedItem) {
		stockChartView.getSelectedStockChart().getStockChartPanel()
		.getStockChartPanelState().setScrip((Scrip)selectedItem);
		EventPublisher.publishEvent(
				new RefreshStockChartDataEvent(
						stockChartView.getSelectedStockChart()));
	}

	@Override
	public int getOrder() {
		return 0;
	}

	@SuppressWarnings("unchecked")
	@Override
	public Collection<Class> getParents() {
		return parentList;
	}

	@Override
	public long getLastRefreshTime() {
		return 0;
	}

	@Override
	public long getRefreshCount() {
		return 0;
	}

	public void setScripManager(ScripManager scripManager) {
		this.scripManager = scripManager;
	}
}
